#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/7/16 17:21
# @File    : tree_view.py
# @Description : -i http://pypi.tuna.tsinghua.edu.cn/simple/ --trusted-host pypi.tuna.tsinghua.edu.cn

import tkinter as tk
import ttkbootstrap as ttk
from random import choice
from ttkbootstrap import utility

utility.enable_high_dpi_awareness()

DARK = 'superhero'
LIGHT = 'flatly'


def create_treeview_style(_, style):
    frame = ttk.Frame(root, padding=5)

    # title
    title = ttk.Label(frame, text='Treeview', anchor=tk.CENTER)
    title.pack(padx=5, pady=2, fill=tk.BOTH)
    ttk.Separator(frame).pack(padx=5, pady=5, fill=tk.X)

    # default
    ttk.Label(frame, text='default').pack(fill=tk.X)
    color = "default"
    style = ttk.Style()
    # 数据字体大小
    #style.configure("Treeview", rowheight=50, font=("Arial", 2, 'bold'))
    # 设置标题字体
    #style.configure("Treeview.Heading", font=("Arial", 12, 'bold'), background="black")
    #style.configure("Treeview", background='red', foreground="green", borderwidth=2, bordercolor="pink")

    #style.configure("Treeview.Heading", font=("Arial", 12, 'bold'), background="black")
    style.configure("Treeview", foreground="green", borderwidth=2, bordercolor="red")
    #style.layout("Treeview", [("Treeview.treearea.x", {"sticky": "nswe", "children": [("Treeview.treearea.y", {"sticky": "nswe", "children": [("Treeitem", {"sticky": "nswe", "children": [("Treeitem.padding", {"sticky": "nswe", "children": [("Treeitem.image", {"sticky": "nswe", "children": [("Treeitem.text", {"sticky": "nswe"})]})]})]})]})]})])


    #tk.Label(foreground=)

    columns = ["A", "B"]
    # hieght = 显示行数
    tv = ttk.Treeview(frame, columns=[0, 1], height=5, bootstyle=color,show='tree headings')
    tv.heading('#0', text='Column', command=lambda :print(123))
    for x in range(2):
        tv.heading(x, text=f'Column {x}', anchor=ttk.CENTER)
        tv.insert('', 'end', text=f'Item {x}',
                  values=[f'Row {x}', f'Row {x + 1}'], tags=x)
        tv.column(column=x, minwidth=100, anchor=ttk.CENTER, stretch=True)  # 设置列
    tv.pack(padx=5, pady=5, fill=tk.BOTH)

    def on_mouse_move(event):
        # 获取鼠标在 tksheet 中的位置
        x, y = event.x, event.y
        print(x, y)
        print(tv.selection_own())

    # 鼠标滑过
    tv.bind("<Motion>", on_mouse_move)

    def on_item_selected(event):
        now = tv.selection()
        print(now)
        print("项被选中")

    # 选中某行
    tv.bind("<<TreeviewSelect>>", on_item_selected)


    # 对表格中的数据设置对应信息  这里的1 就是对应的 tv.insert 中的tags
    tv.tag_configure(1, background="black", foreground="red")

    # colored
    for color in style.colors:
        print(color)


    return frame


def change_style():
    theme = choice(style.theme_names())
    print(theme)
    style.theme_use(theme)


if __name__ == '__main__':
    # create visual widget style tests
    root = tk.Tk()
    style = ttk.Style()

    ttk.Button(text="Change Theme", command=change_style).pack(padx=10, pady=10)

    create_treeview_style('', style).pack(side=tk.LEFT)

    root.mainloop()